Plotly is a really slick interactive graphics frontend, but it uses a different interface than ggplot. Thankfully there’s a trivially easy way to convert a ggplot into a plotly figure!
library(tidyverse)
## ── Attaching packages ───────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2 ✓ purrr 0.3.4
## ✓ tibble 3.0.3 ✓ dplyr 1.0.2
## ✓ tidyr 1.1.2 ✓ stringr 1.4.0
## ✓ readr 1.4.0 ✓ forcats 0.5.0
## ── Conflicts ──────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
Converting a ggplot object to a plotly object is easy with ggplotly(). Note that we can add to the hover text in plotly by setting the text aesthetic.
p1 <-
mtcars %>%
as_tibble(rownames = "model") %>%
ggplot(aes(wt, mpg, text = model)) +
geom_point()
ggplotly(p1)
In order to use interactivity in a knitted document, you need to knit to html format.
Large datasets can be slow with vanilla ggplotly(). An additional call of toWebGL() gives a more scalabale graphic.
p2 <-
diamonds %>%
ggplot(aes(carat, price, color = cut)) +
geom_point()
toWebGL(ggplotly(p2))
## Warning: 'scattergl' objects don't have these attributes: 'hoveron'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'name', 'uid', 'ids', 'customdata', 'meta', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'uirevision', 'x', 'x0', 'dx', 'y', 'y0', 'dy', 'text', 'hovertext', 'textposition', 'textfont', 'mode', 'line', 'marker', 'connectgaps', 'fill', 'fillcolor', 'selected', 'unselected', 'opacity', 'hovertemplate', 'texttemplate', 'error_x', 'error_y', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'metasrc', 'hoverinfosrc', 'xsrc', 'ysrc', 'textsrc', 'hovertextsrc', 'textpositionsrc', 'hovertemplatesrc', 'texttemplatesrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
## Warning: 'scattergl' objects don't have these attributes: 'hoveron'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'name', 'uid', 'ids', 'customdata', 'meta', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'uirevision', 'x', 'x0', 'dx', 'y', 'y0', 'dy', 'text', 'hovertext', 'textposition', 'textfont', 'mode', 'line', 'marker', 'connectgaps', 'fill', 'fillcolor', 'selected', 'unselected', 'opacity', 'hovertemplate', 'texttemplate', 'error_x', 'error_y', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'metasrc', 'hoverinfosrc', 'xsrc', 'ysrc', 'textsrc', 'hovertextsrc', 'textpositionsrc', 'hovertemplatesrc', 'texttemplatesrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
## Warning: 'scattergl' objects don't have these attributes: 'hoveron'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'name', 'uid', 'ids', 'customdata', 'meta', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'uirevision', 'x', 'x0', 'dx', 'y', 'y0', 'dy', 'text', 'hovertext', 'textposition', 'textfont', 'mode', 'line', 'marker', 'connectgaps', 'fill', 'fillcolor', 'selected', 'unselected', 'opacity', 'hovertemplate', 'texttemplate', 'error_x', 'error_y', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'metasrc', 'hoverinfosrc', 'xsrc', 'ysrc', 'textsrc', 'hovertextsrc', 'textpositionsrc', 'hovertemplatesrc', 'texttemplatesrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
## Warning: 'scattergl' objects don't have these attributes: 'hoveron'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'name', 'uid', 'ids', 'customdata', 'meta', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'uirevision', 'x', 'x0', 'dx', 'y', 'y0', 'dy', 'text', 'hovertext', 'textposition', 'textfont', 'mode', 'line', 'marker', 'connectgaps', 'fill', 'fillcolor', 'selected', 'unselected', 'opacity', 'hovertemplate', 'texttemplate', 'error_x', 'error_y', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'metasrc', 'hoverinfosrc', 'xsrc', 'ysrc', 'textsrc', 'hovertextsrc', 'textpositionsrc', 'hovertemplatesrc', 'texttemplatesrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
## Warning: 'scattergl' objects don't have these attributes: 'hoveron'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'name', 'uid', 'ids', 'customdata', 'meta', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'uirevision', 'x', 'x0', 'dx', 'y', 'y0', 'dy', 'text', 'hovertext', 'textposition', 'textfont', 'mode', 'line', 'marker', 'connectgaps', 'fill', 'fillcolor', 'selected', 'unselected', 'opacity', 'hovertemplate', 'texttemplate', 'error_x', 'error_y', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'metasrc', 'hoverinfosrc', 'xsrc', 'ysrc', 'textsrc', 'hovertextsrc', 'textpositionsrc', 'hovertemplatesrc', 'texttemplatesrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'